package com.education.welco.controller.teacherapp;

import com.education.common.pojo.AjaxResult;
import com.education.utils.JwtUtil;
import com.education.websocket.data.HomeBuffer;
import com.education.websocket.data.HomeData;
import com.education.websocket.server.WebSocketServerWELCO;
import com.education.welco.pojo.MessageNotification;
import com.education.welco.service.IMessageNotificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @Author: liftsail
 * @Date: 2023/1/11 16:05
 * @Description: 不积跬步无以至千里
 */
@RestController
@RequestMapping("/apptea/message")
public class AppTeaMessageController {

    @Autowired
    private IMessageNotificationService messageNotificationService;
    @Autowired
    private HomeBuffer homeBuffer;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping("/appTeaTimeNum")
    public AjaxResult list(String teaId) {
        Object data = homeBuffer.data(teaId);
        return AjaxResult.success("查询成功！", data);
    }

    /**
     * 列表查询消息通知
     * teaId=1207&pageNum=1&pageSize=10
     */
    @GetMapping("/list")
    public AjaxResult list(MessageNotification messageNotification, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        //查询消息列表:返回未读和已读消息，排序。点击详情接口后实现已读状态修改
        List<MessageNotification> list = messageNotificationService.selectMessageNotificationList(messageNotification);
        PageInfo pageInfo = new PageInfo<>(list);
        return AjaxResult.success("查询成功！", pageInfo);
    }

    /**
     * 修改消息已读状态&&websocket的数量减1
     * messageId 消息id
     * isRead 0未读 1已读
     */
    @PostMapping("/updateReadState")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateReadState(Integer messageId, Integer teaUserId) {
        MessageNotification result = messageNotificationService.selectMessageNotificationById(messageId);
        if (result.getIsRead().equals(true)) {
            return AjaxResult.success("状态已经修改成功！");
        }
        MessageNotification messageNotification = new MessageNotification();
        messageNotification.setMessageId(messageId);
        messageNotification.setIsRead(true);
        messageNotificationService.updateMessageNotification(messageNotification);
        //websocket的消息已读redis数量更新依据分页查询的数量
        //UserInfo userInfo = jwtUtil.getInfoFromToken();
        //Long teaUserId = userInfo.getId();
        Object res = redisTemplate.opsForValue().get("wait:data:" + teaUserId);
        if (Objects.nonNull(res)) {
            HomeData homeData = (HomeData) res;
            if (homeData.getMessageWait() > 0) {
                homeData.setMessageWait(homeData.getMessageWait() - 1);
            }
            redisTemplate.opsForValue().set("wait:data:" + teaUserId, homeData);
            WebSocketServerWELCO.sendInfo(homeData, String.valueOf(teaUserId));
        } else {
            HomeData homeData = new HomeData();
            homeData.setMessageWait(0);
            redisTemplate.opsForValue().set("wait:data:" + teaUserId, homeData);
            WebSocketServerWELCO.sendInfo(homeData, String.valueOf(teaUserId));
        }
        //更改已阅读消息的状态
        //if (size > 0) {
        //    List<Integer> messageIdList = list.parallelStream().map(messageNotification1 -> messageNotification1.getMessageId()).collect(Collectors.toList());
        //    int i = messageNotificationService.batchUpdateReadState(messageIdList);
        //}
        return AjaxResult.success("状态修改成功！");
    }
}
